package com.heima.controller;


import com.heima.pojo.dto.UserDto;
import com.heima.pojo.dto.UserQueryDto;
import com.heima.pojo.dto.UsersDto;
import com.heima.pojo.po.Users;
import com.heima.pojo.vo.PageVo;
import com.heima.pojo.vo.UserVo;
import com.heima.service.IUserService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 存折
 * @since 2024-10-23
 */
@RestController
@RequestMapping("/api/user-service/user")
public class UserController {
    @Autowired
    private IUserService iuserService;


    @GetMapping("/search")
    public PageVo<UserVo> query(UserDto userDto, UserQueryDto userQueryDto) {
        return iuserService.query(userDto, userQueryDto);
    }

    @PostMapping
    public Boolean add(@RequestBody UsersDto usersDto) {
        Users users = new Users();
        BeanUtils.copyProperties(usersDto, users);
        if (usersDto.getStatus()) {
            users.setStatus(1);
        }
        users.setStatus(0);
        iuserService.save(users);
        return true;
    }

    @PutMapping("/{id}")
    public Boolean update(@PathVariable Integer id, @RequestBody UsersDto usersDto) {
        Users users = new Users();
        BeanUtils.copyProperties(usersDto, users);
        if (usersDto.getStatus()) {
            users.setStatus(1);
        }
        users.setStatus(0);
        users.setId(id);
        iuserService.updateById(users);
        return true;
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        iuserService.removeById(id);
        return true;
    }
}
